/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.server.modules.sys.web.gps;

import com.server.common.config.Global;
import com.server.common.persistence.Page;
import com.server.common.utils.StringUtils;
import com.server.common.web.BaseController;
import com.server.modules.sys.entity.gps.Gps;
import com.server.modules.sys.service.gps.GpsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * GPS信息Controller
 * @author 颜洪毅
 * @version 2016-08-16
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/gps/gps")
public class GpsController extends BaseController {

	@Autowired
	private GpsService gpsService;
	
	@ModelAttribute
	public Gps get(@RequestParam(required=false) String id) {
		Gps entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = gpsService.get(id);
		}
		if (entity == null){
			entity = new Gps();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:gps:gps:view")
	@RequestMapping(value = {"list", ""})
	public String list(Gps gps, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Gps> page = gpsService.findPage(new Page<Gps>(request, response), gps); 
		model.addAttribute("page", page);
		model.addAttribute("gps", gps);
		return "modules/sys/gps/gpsList";
	}

	@RequiresPermissions("sys:gps:gps:view")
	@RequestMapping(value = "form")
	public String form(Gps gps, Model model) {
		model.addAttribute("gps", gps);
		return "modules/sys/gps/gpsForm";
	}

	@RequiresPermissions("sys:gps:gps:edit")
	@RequestMapping(value = "save")
	public String save(Gps gps, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, gps)){
			return form(gps, model);
		}
		gpsService.save(gps);
		addMessage(redirectAttributes, "保存GPS信息成功");
		return "redirect:"+Global.getAdminPath()+"/sys/gps/gps/?repage";
	}
	
	@RequiresPermissions("sys:gps:gps:edit")
	@RequestMapping(value = "delete")
	public String delete(Gps gps, RedirectAttributes redirectAttributes) {
		gpsService.delete(gps);
		addMessage(redirectAttributes, "删除GPS信息成功");
		return "redirect:"+Global.getAdminPath()+"/sys/gps/gps/?repage";
	}

	@RequestMapping(value = "getmap")
	public String getmap(String id,Model model){
//		List<Map<String,Object>> gpsList = gpsService.getmeber(id);
//		model.addAttribute("gpList",gpsList);
		String ids = id;
		model.addAttribute("ids",ids);
		return "modules/sys/gps/gpsmap";
	}

	@RequestMapping(value = "getMaps")
	@ResponseBody
	public Object getMaps(String id){
		List<Map<String,Object>> gpsList = new ArrayList<>();
		if(id ==null){
			return null;
		}else {
			gpsList = gpsService.getmeber(id);
		}
		if(gpsList.size()==0 || gpsList ==null){
			return null;
		}

		return gpsList;
	}
}